﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Ewts.Storage
{
    public partial class VehicleArrangeModify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request["vaid"] != null && Request["vaid"].ToString().Trim() != "")
                {
                    BindPlateNo(Request["vaid"].ToString());
                }
            }
        }


        protected void BindDataCjoid(int cjoid)
        {
            DataLib.Model.CJOItem cjomodel = new DataLib.BLL.CJOItem().GetListByCid(cjoid);
            lblCity.Text = cjomodel.Graphicy;
            lblCjoNo.Text = cjomodel.cjono;
            lblProCode.Text = cjomodel.pronum;
            lblProName.Text = cjomodel.proname;
            lblCustomerName.Text = cjomodel.companyname;
            lblNoticeDate.Text = cjomodel.createtime.ToString();
            lblDateTime.Text = cjomodel.collectdate.ToString();
            lblLoadNum.Text = cjomodel.loadnumno;



            DataLib.Model.VehicleRequire mRequire = null;
            try
            {
                mRequire = new DataLib.BLL.VehicleRequire().GetModel(cjomodel.vehichlerequireid);
            }
            catch
            {
            }
            if (mRequire == null)
            {
                return;
            }
            lblOtherRequire.Text = mRequire.otherrequire;
            lblMaternRequire.Text = mRequire.maternrequire;
            lblPeculiarRequire.Text = mRequire.peculiarrequire;
            lblSamplingRequire.Text = mRequire.samplingrequire;
            lblVehiRequire.Text = mRequire.other;
            lblWorkerCount.Text = mRequire.workercount.ToString();
            lblGoodsCount.Text = mRequire.numberestimate.ToString();
            lblTools.Text = mRequire.tools;

            DataLib.Model.LoadNum loadM = new DataLib.BLL.LoadNum().GetModel(cjomodel.loadnumid);
            lblRubbishArea.Text = loadM.Rubbisharea;
            lblSaleMan.Text = cjomodel.cjoshowmanusername + "- " + cjomodel.cjoshowmantruename;
            lblGoodsWeight.Text = loadM.totalWeight.ToString();
            lblGoodsInfo.Text = cjomodel.cjoremark;

            //DataLib.Model.CumtomerAddress cumAddM = new DataLib.BLL.CumtomerAddress().GetModel((int)loadM.addressid);
            lblAddress.Text = cjomodel.cjoaddinfo;
            lblCustomerContactor.Text = cjomodel.cjolinkman;
            lblCustomerPhoneNum.Text = cjomodel.cjophone;
            //DataLib.Model.Cumtomer cumModel = new DataLib.BLL.Cumtomer().GetModel((int)loadM.customerid);
            //lblCustomerContactor.Text = cumModel.linkman;
            //lblCustomerPhoneNum.Text = cumModel.phone;
            //收货员绑定
            chkVehiclorList.DataSource = new DataLib.BLL.Vehiclor().GetList("city = '" + lblCity.Text.Trim() + "'and isdel=0 ");
            chkVehiclorList.DataTextField = "Name";
            chkVehiclorList.DataValueField = "id";
            chkVehiclorList.DataBind();

            //车牌号绑定
            
            chkPlateNumberList.DataSource = Code.MyCheckBox.GetDataSourceByCity("city = '" + lblCity.Text.Trim() + "'");
            chkPlateNumberList.DataTextField = "ShortName";
            chkPlateNumberList.DataValueField = "id";
            chkPlateNumberList.DataBind();
            foreach (ListItem li in chkPlateNumberList.Items)
            {
                li.Attributes.Add("myvalue", li.Value);
            }
        }

        /// <summary>
        /// 出库单数据绑定
        /// </summary>
        /// <param name="outid"></param>
        public void BindOutid(int outid)
        {
            DataLib.Model.OutStorage outmodel = new DataLib.BLL.OutStorage().GetModel(outid);
            lblCity.Text = outmodel.OutDepart;
            lblCjoNo.Text = outmodel.outStorageNo;

            DataLib.BLL.LoadNum bllLoad = new DataLib.BLL.LoadNum();
            lblProleft.Text = "客户编号";
            lblProCode.Text = bllLoad.GetCusm(outmodel);
            lblCustomerName.Text = bllLoad.GetCusm(outmodel);

            lblNoticeDate.Text = "";
            lblDateTime.Text = outmodel.outStorageDate.ToString();
            lblLoadNum.Text = outmodel.VLoadNum;
            //收货员绑定
            chkVehiclorList.DataSource = new DataLib.BLL.Vehiclor().GetList("city = '" + lblCity.Text.Trim() + "'and isdel=0 ");
            chkVehiclorList.DataTextField = "Name";
            chkVehiclorList.DataValueField = "id";
            chkVehiclorList.DataBind();

            //车牌号绑定
            chkPlateNumberList.DataSource = Code.MyCheckBox.GetDataSourceByCity("city = '" + lblCity.Text.Trim() + "'");
            chkPlateNumberList.DataTextField = "ShortName";
            chkPlateNumberList.DataValueField = "id";
            chkPlateNumberList.DataBind();
            foreach (ListItem li in chkPlateNumberList.Items)
            {
                li.Attributes.Add("myvalue", li.Value);
            }
        }

        public void BindPlateNo(string vaid)
        {

            if (vaid != null && vaid.ToString() != "")
            {
                DataLib.Model.VehicleArrange vamodel = new DataLib.Model.VehicleArrange();
                DataLib.BLL.VehicleArrange bll = new DataLib.BLL.VehicleArrange();
                vamodel = bll.GetModel(int.Parse(vaid));
                lblNo.InnerHtml = "(" + vamodel.vehicleNo + ")";
                if (vamodel.outid != 0)
                {
                    BindOutid((int)vamodel.outid);
                }
                else
                {
                    BindDataCjoid((int)vamodel.cjoid);
                }
                if (vamodel != null)
                {
                    txtvid.Value = vamodel.id.ToString();

                    //收货员选中
                    string[] vehiString = vamodel.vehiderid.Split(',');

                    foreach (string vehi in vehiString)
                    {

                        if (vehi != null && vehi.Trim() != "")
                        {
                            bool flag = false;
                            foreach (System.Web.UI.WebControls.ListItem itemVL in chkVehiclorList.Items)
                            {
                                if (itemVL.Value == vehi)
                                {
                                    flag = true;
                                    itemVL.Selected = true;
                                }
                            }
                            if (flag == false)
                            {
                                ListItem chk = new ListItem();
                                chk.Selected = true;
                                chk.Value = vehi;
                                DataLib.Model.Vehiclor vmodel = new DataLib.Model.Vehiclor();
                                vmodel = new DataLib.BLL.Vehiclor().GetModel(int.Parse(vehi));
                                chk.Text = vmodel.name;
                                chkVehiclorList.Items.Add(chk);
                            }
                        }
                    }

                    //车牌号选中
                    string[] plateString = vamodel.platenumberid.Split(',');
                    foreach (string plate in plateString)
                    {
                        if (plate != null)
                        {
                            foreach (System.Web.UI.WebControls.ListItem itemPN in chkPlateNumberList.Items)
                            {

                                if (itemPN.Value == plate)
                                {
                                    itemPN.Selected = true;
                                }
                            }
                        }
                    }

                    hidSealNo.Value = vamodel.sealno.ToString();
                    string strhtml = "  ";
                    string[] arrSealNo = hidSealNo.Value.Split('@');
                    DataLib.Model.PlateNumber pModel = new DataLib.Model.PlateNumber();
                    DataLib.BLL.PlateNumber pBll = new DataLib.BLL.PlateNumber();
                    if (arrSealNo.Length > 0)
                    {
                        for (int i = 0; i < arrSealNo.Length; i++)
                        {
                            if (arrSealNo[i] != null && arrSealNo[i].Trim() != "")
                            {
                                string[] arrPlateID = arrSealNo[i].Split(':');
                                strhtml += "<span id='span" + arrPlateID[0] + "'>";
                                pModel = pBll.GetModel(int.Parse(plateString[0]));
                                strhtml += "<label>" + pModel.plateno + "：</label>";
                                strhtml += "<input title='" + arrPlateID[0] + "' ";
                                strhtml += " value = '" + arrPlateID[1] + "' class='txtsealno' type='text' id='txtseal_" + arrPlateID[0] + "' onchange='setHidPlateValue(this)' />";
                                strhtml += "</span>";
                            }
                        }
                    }
                    tdSealeValue.InnerHtml = strhtml;
                }

            }

        }

        /// <summary>
        /// 提交事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DataLib.Model.VehicleArrange model = new DataLib.Model.VehicleArrange();
            DataLib.BLL.VehicleArrange bll = new DataLib.BLL.VehicleArrange();
            model= bll.GetModel(int.Parse(txtvid.Value));
            model.platenumberid = Code.MyCheckBox.GetCheckIDColl(chkPlateNumberList, 1, false);
            model.vehiderid = Code.MyCheckBox.GetCheckIDColl(chkVehiclorList, 0, false);
            model.sealno = hidSealNo.Value;
            if (model.platenumberid == null || model.platenumberid.Trim() == "")
            {
                DataLib.Common.MessageBox.Show(Page, "请至少选择一辆车");
                return;
            }
            if (model.vehiderid == null || model.vehiderid.Trim() == "")
            {
                DataLib.Common.MessageBox.Show(Page, "请至少选择一位收货员");
                return;
            }
            if (bll.Update(model) == true)
            {
                string url = "";
                if (model.cjoid != 0)
                {
                    //url = CjoVehicle(model.cjoid.ToString());
                    url = "/RDLC/CjoReport.aspx?cjoid=" + model.cjoid.ToString();
                    DataLib.Common.MessageBox.ShowAndRedirect(this, "修改成功，打印", url);
                }
                else
                {
                    DataLib.BLL.OutStorage outbll = new DataLib.BLL.OutStorage();
                    DataLib.Model.OutStorage outmodel = outbll.GetModel(model.outid);
                    if (outmodel.status == 2)
                    {
                        if ((outmodel.inDepart == outmodel.belogarea)
                            //               && (outmodel.OutDepart != outmodel.graphicCity)
                            && (outmodel.StorageType == "非原料库"))
                        {
                            outmodel.status = 4;
                        }
                        else
                        {
                            outmodel.status = 3;
                        }
                    }
                    outbll.Update(outmodel);
                    //url = OutVehicle(model.outid.ToString());
                    url = "/RDLC/CjoReport.aspx?outid=" + model.outid.ToString();
                    DataLib.Common.MessageBox.ShowAndRedirect(this, "修改成功，打印", url);
                }

            }

        }

        protected string CjoVehicle(string cjoid)
        {
            string url = "/RDLC/CjoReport.aspx?cjoid=";
            DataLib.Model.VehicleArrange model = new DataLib.Model.VehicleArrange();
            DataLib.BLL.VehicleArrange bll = new DataLib.BLL.VehicleArrange();
            model = bll.GetModelBySrcid(cjoid, 1);
            model.cjoid = int.Parse(cjoid);
            model.sealno = hidSealNo.Value;
            model.status = 1;
            model.outid = 0;
            model.platenumberid = Code.MyCheckBox.GetCheckIDColl(chkPlateNumberList, 1);
            model.vehiderid = Code.MyCheckBox.GetCheckIDColl(chkVehiclorList, 0);
            if (model.platenumberid == null || model.platenumberid.Trim() == "")
            {
                DataLib.Common.MessageBox.Show(Page, "请至少选择一辆车");
                return "-1";
            }
            if (model.vehiderid == null || model.vehiderid.Trim() == "")
            {
                DataLib.Common.MessageBox.Show(Page, "请至少选择一位收货员");
                return "-1";
            }
            DataLib.Model.Cjo cmodel = new DataLib.BLL.Cjo().GetModel(int.Parse(cjoid));
            cmodel.status = 2;
            new DataLib.BLL.Cjo().Update(cmodel);
            if (new DataLib.BLL.VehicleArrange().Add(model) > 0)
            {
                url += cjoid;
            }
            else
            {
                url = "-1";
            }
            return url;
        }

        protected string OutVehicle(string outid)
        {
            string url = "/RDLC/CjoReport.aspx?outid=";
            DataLib.Model.VehicleArrange model = new DataLib.Model.VehicleArrange();
            DataLib.BLL.VehicleArrange bll = new DataLib.BLL.VehicleArrange();
            model = bll.GetModelBySrcid(outid, 0);
            model.outid = int.Parse(outid);
            model.cjoid = 0;
            model.sealno = hidSealNo.Value;
            model.status = 1;
            model.platenumberid = Code.MyCheckBox.GetCheckIDColl(chkPlateNumberList, 1);
            model.vehiderid = Code.MyCheckBox.GetCheckIDColl(chkVehiclorList, 0);
            if (model.platenumberid == null || model.platenumberid.Trim() == "")
            {
                DataLib.Common.MessageBox.Show(Page, "请至少选择一辆车");
                return "-1";
            }
            if (model.vehiderid == null || model.vehiderid.Trim() == "")
            {
                DataLib.Common.MessageBox.Show(Page, "请至少选择一位收货员");
                return "-1";
            }
            DataLib.Model.OutStorage outmodel = new DataLib.BLL.OutStorage().GetModel(int.Parse(outid));

            //if ((outmodel.inDepart == outmodel.belogarea) && (outmodel.OutDepart != outmodel.OutDepart)&&(outmodel.StorageType=="非原料库"))
            //{
            //    outmodel.status = 4;
            //}
            //else
            //{
            //    outmodel.status = 3;
            //}

            if ((outmodel.inDepart == outmodel.belogarea)
                //               && (outmodel.OutDepart != outmodel.graphicCity)
                  && (outmodel.StorageType == "非原料库"))
            {
                outmodel.status = 4;
            }
            else
            {
                outmodel.status = 3;
            }
            new DataLib.BLL.OutStorage().Update(outmodel);
            if (bll.Update(model))
            {
                url += outid;
            }
            else
            {
                url = "-1";
            }
            return url;
        }

        //public void bindItemPlate(DataTable dt)
        //{
        //    ListItem lt;
        //    if (dt != null && dt.Rows.Count > 0)
        //    { 
        //        foreach (ListItem li in cbxStyle.Items)
        //    {
        //        li.Attributes.Add("alt", li.Value);
        //    }
        //        {
        //            lt = new ListItem(dt.Rows[i]["ShortName"].ToString(), dt.Rows[i]["id"].ToString());
        //            lt.Attributes.Add(
        //            lt.Attributes["myvalue"] = dt.Rows[i]["id"].ToString();
        //            chkPlateNumberList.Items.Add(lt);
        //        }
        //    }
        //}
    }
}